package com.tz.farmer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tz.farmer.model.FarmerNutrition;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface FarmerNutritionMapper extends BaseMapper<FarmerNutrition> {
    int deleteByPrimaryKey(Integer id);

    int insert(FarmerNutrition record);

    int insertSelective(FarmerNutrition record);

    FarmerNutrition selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(FarmerNutrition record);

    int updateByPrimaryKey(FarmerNutrition record);

    /**
     * kai 置顶之前查询top最大值
     * @param flag
     * @return
     */
    @Select("SELECT  " +
            "\tmax( top ) AS maxTop \n" +
            "FROM\n" +
            "\tfarmer_nutrition \n" +
            "WHERE\n" +
            "\tproperty = #{flag}")
    int selectMaxTop(@Param("flag") Integer flag);


    /**
     * kai 总条数
     * @return
     */
    @Select("<script>" +
            "select   \n" +
            "count(1) \n" +
            "from farmer_nutrition \n" +
            "where property = #{flag} and 1 = 1  " +
            " <if test = \"keyword != null and keyword.trim() !=''  \"  >and nutritive like CONCAT('%', #{keyword},'%') </if> \n" +
            " <if test = \"classify != null and classify.trim() !=''  \" >and classify like CONCAT('%', #{classify},'%')  </if> \n" +
            " <if test=\"startTime != null and startTime.trim() != ''\">" +
            "   <![CDATA[   and ( STR_TO_DATE(release_time, '%Y-%m-%d %H:%i:%s') >=  STR_TO_DATE( '${startTime} 00:00:00', '%Y-%m-%d %H:%i:%s')    \n" +
            "     and STR_TO_DATE(release_time, '%Y-%m-%d %H:%i:%s') <= STR_TO_DATE( '${endTime} 23:59:59', '%Y-%m-%d %H:%i:%s')  )  ]]> \n" +
            " </if> " +
            "</script>")
    int countFlag(@Param("flag")Integer flag,@Param("classify") String classify,@Param("keyword") String keyword,@Param("startTime") String startTime, @Param("endTime")String endTime);

    /**
     * kai 条件查询
     * @return
     */
    @Select("<script>" +
            "select   \n" +
            " id, \n" +
            " classify, \n" +
            " title, \n" +
            " `name`, \n" +
            " p_pic, \n" +
            " nutritive, \n" +
            " c_pic, \n" +
            " c_name, \n" +
            " c_ingredient, \n" +
            " c_step, \n" +
            " is_sued, \n" +
            " property, \n" +
            " release_time, \n" +
            " select_num, \n" +
            " `user`, \n" +
            " top\n" +
            " FROM\n" +
            " farmer_nutrition" +
            " where property = #{flag} and 1 = 1  " +
            " <if test = \"keyword != null and keyword.trim() !=''  \"  >and nutritive like CONCAT('%', #{keyword},'%') </if> \n" +
            " <if test = \"classify != null and classify.trim() !=''  \"  >and classify like CONCAT('%', #{classify},'%')  </if> \n" +
            " <if test=\"startTime != null and startTime.trim() != ''\">" +
            "   <![CDATA[   and ( STR_TO_DATE(release_time, '%Y-%m-%d %H:%i:%s') >=  STR_TO_DATE( '${startTime} 00:00:00', '%Y-%m-%d %H:%i:%s')  \n" +
            "     and STR_TO_DATE(release_time, '%Y-%m-%d %H:%i:%s') <= STR_TO_DATE( '${endTime} 23:59:59', '%Y-%m-%d %H:%i:%s')  )  ]]> \n" +
            " </if> " +
            " order by top DESC , release_time DESC limit #{n} , #{m} " +
            "</script>")
    List<FarmerNutrition> selectConditionList(@Param("flag")Integer flag,@Param("classify") String classify,@Param("keyword") String keyword,@Param("startTime") String startTime, @Param("endTime")String endTime, @Param("n") int n,@Param("m") int m);

}